package com.hup.utils.swing.demos;

import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;

@Log4j2
@SuppressWarnings({"all"})
public class JListDemo2 extends JPanel {
    final String[] DATA = {"One\n1",
            "Two\n2", "Three\n3"
    };
    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> list = new JList<>(listModel);

    public JListDemo2() {
        list.setCellRenderer(new MyPanel());

        for (String datum : DATA) {
            listModel.addElement(datum);
        }
        add(new JScrollPane(list));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JListDemo2());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class Listm extends JTextArea implements ListCellRenderer {
        protected Listm(int rows, int cols) {
            super(rows, cols);
            setBorder(BorderFactory.createLineBorder(Color.blue));
        }

        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            setText(value.toString());
            if (cellHasFocus) {
                setBackground(Color.RED);
            } else if (isSelected) {
                setBackground(Color.BLUE);
            } else {
                setBackground(null);
            }

            log.info("value={}", value);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JLabel("aa"), BorderLayout.NORTH);
            panel.add(this);
            return panel;
        }
    }
    private static class MyPanel extends JPanel implements ListCellRenderer {

        private JTextArea ta;

        private MyPanel() {
            super(new BorderLayout());
            add(new JLabel("aa"), BorderLayout.NORTH);
            ta = new JTextArea();
            add(ta);


            Dimension size = new Dimension(250, 100);
            setPreferredSize(size);
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            //MyPanel res = new MyPanel();
            //res.ta.setText(String.valueOf(value));
            //return res;
            ta.setText(String.valueOf(value));
            return this;
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
        }
    }
}